package javacafe.action.vote;

import java.util.List;

import javacafe.action.Action;
import javacafe.action.ActionForward;
import javacafe.dto.member.LoginInfoDTO;
import javacafe.dto.vote.VoteDTO;
import javacafe.dto.vote.VotePlayDTO;
import javacafe.serviceimple.VoteServiceImple;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class VoteUpdateCountAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();		
		VoteServiceImple service = VoteServiceImple.getInstance();		
		VoteDTO vdto = new VoteDTO();
		VoteDTO dto = new VoteDTO();
		VotePlayDTO bvpdto = new VotePlayDTO();
		
		String vote_no = request.getParameter("seq");
		String voteitem = request.getParameter("voteitem");	
		
		dto.setVote_no(Integer.parseInt(vote_no));
		vdto.setVote_no(Integer.parseInt(vote_no));
		bvpdto.setVote_no(Integer.parseInt(vote_no));
		bvpdto.setM_no(((LoginInfoDTO)request.getSession().getAttribute("loginInfo")).getM_no());
		
		dto = service.getVote(dto);
		vdto = service.getVote(vdto);		
		bvpdto = service.getVotePlay(bvpdto);
			

		if(bvpdto==null){
			VotePlayDTO vpdto = new VotePlayDTO();
			if(voteitem.equalsIgnoreCase(vdto.getVote_item1())){
				dto.setVote_itemcount1(vdto.getVote_itemcount1()+1);
				vpdto.setVoteplay(voteitem);
							
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item2())){
				dto.setVote_itemcount2(vdto.getVote_itemcount2()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item3())){
				dto.setVote_itemcount3(vdto.getVote_itemcount3()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item4())){
				dto.setVote_itemcount1(vdto.getVote_itemcount4()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item5())){
				dto.setVote_itemcount5(vdto.getVote_itemcount5()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item6())){
				dto.setVote_itemcount6(vdto.getVote_itemcount6()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item7())){
				dto.setVote_itemcount1(vdto.getVote_itemcount7()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item8())){
				dto.setVote_itemcount1(vdto.getVote_itemcount8()+1);
				vpdto.setVoteplay(voteitem);
				
			}else if(voteitem.equalsIgnoreCase(vdto.getVote_item9())){
				dto.setVote_itemcount1(vdto.getVote_itemcount9()+1);
				vpdto.setVoteplay(voteitem);				
			}
			service.updateVote(dto);
			
			vpdto.setM_no(((LoginInfoDTO)request.getSession().getAttribute("loginInfo")).getM_no());
			vpdto.setVote_no(dto.getVote_no());		
			
			service.insertVotePlay(vpdto);
			
			dto = service.getVote(dto);				
			request.setAttribute("dto",dto);
			actfoward.setUrl("detailvote");					
			
		}else{			
			List<VoteDTO> list = null;			
			list = service.getVoteList();			
			request.setAttribute("list",list);			
			actfoward.setUrl("votelistform");				
		}			
		
		return actfoward;
	}

}
